import qb_database
import json
import time
import datetime


K_M_dict = {"All_run":[], "Katana":[], "MR":[], "Time":[]}

# def get_the_data_dept():
#     department_dict = {}
#     job_info = q.job(query_field="id,status,name,cpustally,prod_dept", query="status = 336 ")
#     for index,job in enumerate(job_info):
#         if not department_dict.has_key(job['prod_dept']):
#             department_dict[job['prod_dept']] = 0
#         if job['prod_dept'] in department_dict.keys():
#             running_node = int(job['cpustally'].split(' ')[0])
#             department_dict[job['prod_dept']] += running_node
#     print department_dict


def get_the_data_k_M():
    katana_r_count = q.Katana_subjob_r_count()
    MR_r_count = q.MR_subjob_r_count()
    All_t_count = q.subjob_r_count()
    return All_t_count,katana_r_count,MR_r_count


if __name__ == "__main__":
    starttime = datetime.datetime(2020, 4, 27, 9, 0, 0)
    while datetime.datetime.now() < starttime:
        time.sleep(1)
    count = 0
    while True:
        count += 1
        print 'start---record--%s'%(count)
        q = qb_database.QubeDatabase()
        All_t_count,katana_r_count, MR_r_count = get_the_data_k_M()
        K_M_dict["All_run"].append(All_t_count)
        K_M_dict["Katana"].append(katana_r_count)
        K_M_dict["MR"].append(MR_r_count)
        K_M_dict["Time"].append(time.strftime("%H", time.localtime(time.time())))

        if len(K_M_dict["All_run"]) == 10:
            file_path = '/show/WDN/misc/dept/lgt/RenderFarm/archive/Classification_report/daily_report/data_json/night/'
            with open(file_path + '%s_machine_usage.json' % time.strftime("%Y-%m-%d", time.localtime(time.time())),
                      'w') as f:
                jason_strr = json.dumps(K_M_dict)
                f.write(jason_strr)
                print 'write night is ok'
        if len(K_M_dict["All_run"]) == 24:
            file_path = '/show/WDN/misc/dept/lgt/RenderFarm/archive/Classification_report/daily_report/data_json/'
            with open(file_path+'%s_machine_usage.json'%time.strftime("%Y-%m-%d", time.localtime(time.time())), 'w') as f:
                jason_strr = json.dumps(K_M_dict)
                f.write(jason_strr)
                print 'write morning is ok'
            K_M_dict = {"All_run":[], "Katana":[], "MR":[], "Time":[]}
        time.sleep(3600)





